package com.wfx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wfx.entity.*;
import com.wfx.mapper.WxbGoodsTemplateMapper;
import com.wfx.service.IWxbGoodsTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhu
 * @since 2021-01-21
 */
@Service
public class WxbGoodsTemplateServiceImpl extends ServiceImpl<WxbGoodsTemplateMapper, WxbGoodsTemplate> implements IWxbGoodsTemplateService {

    @Autowired
    private WxbGoodsTemplateMapper wxbGoodsTemplateMapper;

    //条件加分页
    @Override
    public PageResult<WxbGoodsTemplate> search(PageDTO<WxbGoodsTemplate> pageDTO) {

        IPage<WxbGoodsTemplate> iPage = new Page<>(pageDTO.getPage(),pageDTO.getLimit());

        QueryWrapper<WxbGoodsTemplate> queryWrapper = new QueryWrapper<>();

        WxbGoodsTemplate data = pageDTO.getData();

        PageResult pageResult = new PageResult<>();

        if (!StringUtils.isEmpty(data.getName())) {
            queryWrapper.eq("name",data.getName());
        }

        IPage<WxbGoodsTemplate> pageInfo = wxbGoodsTemplateMapper.selectPage(iPage, queryWrapper);
        pageResult.setSuccess(true);
        pageResult.setData(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setMsg("查询成功");
        return pageResult;
    }
    //添加or修改
    @Override
    public Result addOrUpdate(WxbGoodsTemplate wxbGoodsTemplate) {

        if (wxbGoodsTemplate == null) {
            return new Result(false,"参数异常");
        }
        if(!StringUtils.isEmpty(wxbGoodsTemplate.getId())){
            int i = wxbGoodsTemplateMapper.updateById(wxbGoodsTemplate);
            return  new Result(i==1?true:false,i==1?"修改成功":"修改失败");
        }else {
            int i = wxbGoodsTemplateMapper.insert(wxbGoodsTemplate);
            return  new Result(i==1?true:false,i==1?"添加成功":"添加失败");
        }
    }
    //批量删除
    @Override
    public Result delTemplates(List<String> Templates) {
        if (Templates == null) {
            return new Result(false,"参数异常");
        }
        int i = wxbGoodsTemplateMapper.deleteBatchIds(Templates);
        return  new Result(i==1?true:false,i==1?"删除成功":"删除失败");
    }
    //删除
    @Override
    public Result delOne(WxbGoodsTemplate wxbGoodsTemplate) {
        if (wxbGoodsTemplate == null) {
            return new Result(false,"参数异常");
        }
        int i = wxbGoodsTemplateMapper.deleteById(wxbGoodsTemplate);
        return  new Result(i==1?true:false,i==1?"删除成功":"删除失败");
    }

    @Override
    public Result searchAll() {
        QueryWrapper queryWrapper = new QueryWrapper();
        List list = wxbGoodsTemplateMapper.selectList(queryWrapper);
        return new Result(true,"厉害的甲方妈妈",list);
    }

}
